home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python151_Src.lha / Python1.5_Source / Objects / intobject.c < prev    next >
C/C++ Source or Header  |  1998-01-26  |  17KB  |  812 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Integer object implementation */
  33.  
  34. #include "Python.h"
  35.  
  36. #ifdef HAVE_LIMITS_H
  37. #include <limits.h>
  38. #endif
  39.  
  40. #include "protos/intobject_protos.h"
  41.  
  42. #ifndef LONG_MAX
  43. #define LONG_MAX 0X7FFFFFFFL
  44. #endif
  45.  
  46. #ifndef LONG_MIN
  47. #define LONG_MIN (-LONG_MAX-1)
  48. #endif
  49.  
  50. #ifndef CHAR_BIT
  51. #define CHAR_BIT 8
  52. #endif
  53.  
  54. #ifndef LONG_BIT
  55. #define LONG_BIT (CHAR_BIT * sizeof(long))
  56. #endif
  57.  
  58. long
  59. PyInt_GetMax()
  60. {
  61.     return LONG_MAX;    /* To initialize sys.maxint */
  62. }
  63.  
  64. /* Standard Booleans */
  65.  
  66. PyIntObject _Py_ZeroStruct = {
  67.     PyObject_HEAD_INIT(&PyInt_Type)
  68.     0
  69. };
  70.  
  71. PyIntObject _Py_TrueStruct = {
  72.     PyObject_HEAD_INIT(&PyInt_Type)
  73.     1
  74. };
  75.  
  76. static PyObject *
  77. err_ovf(msg)
  78.     char *msg;
  79. {
  80.     PyErr_SetString(PyExc_OverflowError, msg);
  81.     return NULL;
  82. }
  83.  
  84. /* Integers are quite normal objects, to make object handling uniform.
  85.    (Using odd pointers to represent integers would save much space
  86.    but require extra checks for this special case throughout the code.)
  87.    Since, a typical Python program spends much of its time allocating
  88.    and deallocating integers, these operations should be very fast.
  89.    Therefore we use a dedicated allocation scheme with a much lower
  90.    overhead (in space and time) than straight malloc(): a simple
  91.    dedicated free list, filled when necessary with memory from malloc().
  92. */
  93.  
  94. #define BLOCK_SIZE    1000    /* 1K less typical malloc overhead */
  95. #define N_INTOBJECTS    (BLOCK_SIZE / sizeof(PyIntObject))
  96.  
  97. static PyIntObject *
  98. fill_free_list()
  99. {
  100.     PyIntObject *p, *q;
  101.     p = PyMem_NEW(PyIntObject, N_INTOBJECTS);
  102.     if (p == NULL)
  103.         return (PyIntObject *)PyErr_NoMemory();
  104.     q = p + N_INTOBJECTS;
  105.     while (--q > p)
  106.         *(PyIntObject **)q = q-1;
  107.     *(PyIntObject **)q = NULL;
  108.     return p + N_INTOBJECTS - 1;
  109. }
  110.  
  111. static PyIntObject *free_list = NULL;
  112. #ifndef NSMALLPOSINTS
  113. #define NSMALLPOSINTS        100
  114. #endif
  115. #ifndef NSMALLNEGINTS
  116. #define NSMALLNEGINTS        1
  117. #endif
  118. #if NSMALLNEGINTS + NSMALLPOSINTS > 0
  119. /* References to small integers are saved in this array so that they
  120.    can be shared.
  121.    The integers that are saved are those in the range
  122.    -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
  123. */
  124. static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
  125. #endif
  126. #ifdef COUNT_ALLOCS
  127. int quick_int_allocs, quick_neg_int_allocs;
  128. #endif
  129.  
  130. PyObject *
  131. PyInt_FromLong(ival)
  132.     long ival;
  133. {
  134.     register PyIntObject *v;
  135. #if NSMALLNEGINTS + NSMALLPOSINTS > 0
  136.     if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS &&
  137.         (v = small_ints[ival + NSMALLNEGINTS]) != NULL) {
  138.         Py_INCREF(v);
  139. #ifdef COUNT_ALLOCS
  140.         if (ival >= 0)
  141.             quick_int_allocs++;
  142.         else
  143.             quick_neg_int_allocs++;
  144. #endif
  145.         return (PyObject *) v;
  146.     }
  147. #endif
  148.     if (free_list == NULL) {
  149.         if ((free_list = fill_free_list()) == NULL)
  150.             return NULL;
  151.     }
  152.     v = free_list;
  153.     free_list = *(PyIntObject **)free_list;
  154.     v->ob_type = &PyInt_Type;
  155.     v->ob_ival = ival;
  156.     _Py_NewReference(v);
  157. #if NSMALLNEGINTS + NSMALLPOSINTS > 0
  158.     if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
  159.         /* save this one for a following allocation */
  160.         Py_INCREF(v);
  161.         small_ints[ival + NSMALLNEGINTS] = v;
  162.     }
  163. #endif
  164.     return (PyObject *) v;
  165. }
  166.  
  167. static void
  168. int_dealloc(v)
  169.     PyIntObject *v;
  170. {
  171.     *(PyIntObject **)v = free_list;
  172.     free_list = v;
  173. }
  174.  
  175. long
  176. PyInt_AsLong(op)
  177.     register PyObject *op;
  178. {
  179.     PyNumberMethods *nb;
  180.     PyIntObject *io;
  181.     long val;
  182.     
  183.     if (op && PyInt_Check(op))
  184.         return PyInt_AS_LONG((PyIntObject*) op);
  185.     
  186.     if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
  187.         nb->nb_int == NULL) {
  188.         PyErr_BadArgument();
  189.         return -1;
  190.     }
  191.     
  192.     io = (PyIntObject*) (*nb->nb_int) (op);
  193.     if (io == NULL)
  194.         return -1;
  195.     if (!PyInt_Check(io)) {
  196.         PyErr_SetString(PyExc_TypeError,
  197.                 "nb_int should return int object");
  198.         return -1;
  199.     }
  200.     
  201.     val = PyInt_AS_LONG(io);
  202.     Py_DECREF(io);
  203.     
  204.     return val;
  205. }
  206.  
  207. /* Methods */
  208.  
  209. /* ARGSUSED */
  210. static int
  211. int_print(v, fp, flags)
  212.     PyIntObject *v;
  213.     FILE *fp;
  214.     int flags; /* Not used but required by interface */
  215. {
  216.     fprintf(fp, "%ld", v->ob_ival);
  217.     return 0;
  218. }
  219.  
  220. static PyObject *
  221. int_repr(v)
  222.     PyIntObject *v;
  223. {
  224.     char buf[20];
  225.     sprintf(buf, "%ld", v->ob_ival);
  226.     return PyString_FromString(buf);
  227. }
  228.  
  229. static int
  230. int_compare(v, w)
  231.     PyIntObject *v, *w;
  232. {
  233.     register long i = v->ob_ival;
  234.     register long j = w->ob_ival;
  235.     return (i < j) ? -1 : (i > j) ? 1 : 0;
  236. }
  237.  
  238. static long
  239. int_hash(v)
  240.     PyIntObject *v;
  241. {
  242.     /* XXX If this is changed, you also need to change the way
  243.        Python's long, float and complex types are hashed. */
  244.     long x = v -> ob_ival;
  245.     if (x == -1)
  246.         x = -2;
  247.     return x;
  248. }
  249.  
  250. static PyObject *
  251. int_add(v, w)
  252.     PyIntObject *v;
  253.     PyIntObject *w;
  254. {
  255.     register long a, b, x;
  256.     a = v->ob_ival;
  257.     b = w->ob_ival;
  258.     x = a + b;
  259.     if ((x^a) < 0 && (x^b) < 0)
  260.         return err_ovf("integer addition");
  261.     return PyInt_FromLong(x);
  262. }
  263.  
  264. static PyObject *
  265. int_sub(v, w)
  266.     PyIntObject *v;
  267.     PyIntObject *w;
  268. {
  269.     register long a, b, x;
  270.     a = v->ob_ival;
  271.     b = w->ob_ival;
  272.     x = a - b;
  273.     if ((x^a) < 0 && (x^~b) < 0)
  274.         return err_ovf("integer subtraction");
  275.     return PyInt_FromLong(x);
  276. }
  277.  
  278. /*
  279. Integer overflow checking used to be done using a double, but on 64
  280. bit machines (where both long and double are 64 bit) this fails
  281. because the double doesn't have enouvg precision.  John Tromp suggests
  282. the following algorithm:
  283.  
  284. Suppose again we normalize a and b to be nonnegative.
  285. Let ah and al (bh and bl) be the high and low 32 bits of a (b, resp.).
  286. Now we test ah and bh against zero and get essentially 3 possible outcomes.
  287.  
  288. 1) both ah and bh > 0 : then report overflow
  289.  
  290. 2) both ah and bh = 0 : then compute a*b and report overflow if it comes out
  291.                         negative
  292.  
  293. 3) ah > 0 and bh = 0  : compute ah*bl and report overflow if it's >= 2^31
  294.                         compute al*bl and report overflow if it's negative
  295.                         add (ah*bl)<<32 to al*bl and report overflow if
  296.                         it's negative
  297.  
  298. In case of no overflow the result is then negated if necessary.
  299.  
  300. The majority of cases will be 2), in which case this method is the same as
  301. what I suggested before. If multiplication is expensive enough, then the
  302. other method is faster on case 3), but also more work to program, so I
  303. guess the above is the preferred solution.
  304.  
  305. */
  306.  
  307. static PyObject *
  308. int_mul(v, w)
  309.     PyIntObject *v;
  310.     PyIntObject *w;
  311. {
  312.     long a, b, ah, bh, x, y;
  313.     int s = 1;
  314.  
  315.     a = v->ob_ival;
  316.     b = w->ob_ival;
  317.     ah = a >> (LONG_BIT/2);
  318.     bh = b >> (LONG_BIT/2);
  319.  
  320.     /* Quick test for common case: two small positive ints */
  321.  
  322.     if (ah == 0 && bh == 0) {
  323.         x = a*b;
  324.         if (x < 0)
  325.             goto bad;
  326.         return PyInt_FromLong(x);
  327.     }
  328.  
  329.     /* Arrange that a >= b >= 0 */
  330.  
  331.     if (a < 0) {
  332.         a = -a;
  333.         if (a < 0) {
  334.             /* Largest negative */
  335.             if (b == 0 || b == 1) {
  336.                 x = a*b;
  337.                 goto ok;
  338.             }
  339.             else
  340.                 goto bad;
  341.         }
  342.         s = -s;
  343.         ah = a >> (LONG_BIT/2);
  344.     }
  345.     if (b < 0) {
  346.         b = -b;
  347.         if (b < 0) {
  348.             /* Largest negative */
  349.             if (a == 0 || (a == 1 && s == 1)) {
  350.                 x = a*b;
  351.                 goto ok;
  352.             }
  353.             else
  354.                 goto bad;
  355.         }
  356.         s = -s;
  357.         bh = b >> (LONG_BIT/2);
  358.     }
  359.  
  360.     /* 1) both ah and bh > 0 : then report overflow */
  361.  
  362.     if (ah != 0 && bh != 0)
  363.         goto bad;
  364.  
  365.     /* 2) both ah and bh = 0 : then compute a*b and report
  366.                    overflow if it comes out negative */
  367.  
  368.     if (ah == 0 && bh == 0) {
  369.         x = a*b;
  370.         if (x < 0)
  371.             goto bad;
  372.         return PyInt_FromLong(x*s);
  373.     }
  374.  
  375.     if (a < b) {
  376.         /* Swap */
  377.         x = a;
  378.         a = b;
  379.         b = x;
  380.         ah = bh;
  381.         /* bh not used beyond this point */
  382.     }
  383.  
  384.     /* 3) ah > 0 and bh = 0  : compute ah*bl and report overflow if
  385.                    it's >= 2^31
  386.                         compute al*bl and report overflow if it's negative
  387.                         add (ah*bl)<<32 to al*bl and report overflow if
  388.                         it's negative
  389.             (NB b == bl in this case, and we make a = al) */
  390.  
  391.     y = ah*b;
  392.     if (y >= (1L << (LONG_BIT/2 - 1)))
  393.         goto bad;
  394.     a &= (1L << (LONG_BIT/2)) - 1;
  395.     x = a*b;
  396.     if (x < 0)
  397.         goto bad;
  398.     x += y << (LONG_BIT/2);
  399.     if (x < 0)
  400.         goto bad;
  401.  ok:
  402.     return PyInt_FromLong(x * s);
  403.  
  404.  bad:
  405.     return err_ovf("integer multiplication");
  406. }
  407.  
  408. static int
  409. i_divmod(x, y, p_xdivy, p_xmody)
  410.     register PyIntObject *x, *y;
  411.     long *p_xdivy, *p_xmody;
  412. {
  413.     long xi = x->ob_ival;
  414.     long yi = y->ob_ival;
  415.     long xdivy, xmody;
  416.     
  417.     if (yi == 0) {
  418.         PyErr_SetString(PyExc_ZeroDivisionError,
  419.                 "integer division or modulo");
  420.         return -1;
  421.     }
  422.     if (yi < 0) {
  423.         if (xi < 0)
  424.             xdivy = -xi / -yi;
  425.         else
  426.             xdivy = - (xi / -yi);
  427.     }
  428.     else {
  429.         if (xi < 0)
  430.             xdivy = - (-xi / yi);
  431.         else
  432.             xdivy = xi / yi;
  433.     }
  434.     xmody = xi - xdivy*yi;
  435.     if ((xmody < 0 && yi > 0) || (xmody > 0 && yi < 0)) {
  436.         xmody += yi;
  437.         xdivy -= 1;
  438.     }
  439.     *p_xdivy = xdivy;
  440.     *p_xmody = xmody;
  441.     return 0;
  442. }
  443.  
  444. static PyObject *
  445. int_div(x, y)
  446.     PyIntObject *x;
  447.     PyIntObject *y;
  448. {
  449.     long d, m;
  450.     if (i_divmod(x, y, &d, &m) < 0)
  451.         return NULL;
  452.     return PyInt_FromLong(d);
  453. }
  454.  
  455. static PyObject *
  456. int_mod(x, y)
  457.     PyIntObject *x;
  458.     PyIntObject *y;
  459. {
  460.     long d, m;
  461.     if (i_divmod(x, y, &d, &m) < 0)
  462.         return NULL;
  463.     return PyInt_FromLong(m);
  464. }
  465.  
  466. static PyObject *
  467. int_divmod(x, y)
  468.     PyIntObject *x;
  469.     PyIntObject *y;
  470. {
  471.     long d, m;
  472.     if (i_divmod(x, y, &d, &m) < 0)
  473.         return NULL;
  474.     return Py_BuildValue("(ll)", d, m);
  475. }
  476.  
  477. static PyObject *
  478. int_pow(v, w, z)
  479.     PyIntObject *v;
  480.     PyIntObject *w;
  481.     PyIntObject *z;
  482. {
  483. #if 1
  484.     register long iv, iw, iz=0, ix, temp, prev;
  485.     iv = v->ob_ival;
  486.     iw = w->ob_ival;
  487.     if (iw < 0) {
  488.         PyErr_SetString(PyExc_ValueError,
  489.                 "integer to the negative power");
  490.         return NULL;
  491.     }
  492.      if ((PyObject *)z != Py_None) {
  493.         iz = z->ob_ival;
  494.         if (iz == 0) {
  495.             PyErr_SetString(PyExc_ValueError,
  496.                     "pow(x, y, z) with z==0");
  497.             return NULL;
  498.         }
  499.     }
  500.     /*
  501.      * XXX: The original exponentiation code stopped looping
  502.      * when temp hit zero; this code will continue onwards
  503.      * unnecessarily, but at least it won't cause any errors.
  504.      * Hopefully the speed improvement from the fast exponentiation
  505.      * will compensate for the slight inefficiency.
  506.      * XXX: Better handling of overflows is desperately needed.
  507.      */
  508.      temp = iv;
  509.     ix = 1;
  510.     while (iw > 0) {
  511.          prev = ix;    /* Save value for overflow check */
  512.          if (iw & 1) {    
  513.              ix = ix*temp;
  514.             if (temp == 0)
  515.                 break; /* Avoid ix / 0 */
  516.             if (ix / temp != prev)
  517.                 return err_ovf("integer pow()");
  518.         }
  519.          iw >>= 1;    /* Shift exponent down by 1 bit */
  520.             if (iw==0) break;
  521.          prev = temp;
  522.          temp *= temp;    /* Square the value of temp */
  523.          if (prev!=0 && temp/prev!=prev)
  524.             return err_ovf("integer pow()");
  525.          if (iz) {
  526.             /* If we did a multiplication, perform a modulo */
  527.              ix = ix % iz;
  528.              temp = temp % iz;
  529.         }
  530.     }
  531.     if (iz) {
  532.          PyObject *t1, *t2;
  533.          long int div, mod;
  534.          t1=PyInt_FromLong(ix); 
  535.         t2=PyInt_FromLong(iz);
  536.          if (t1==NULL || t2==NULL ||
  537.              i_divmod((PyIntObject *)t1,
  538.                  (PyIntObject *)t2, &div, &mod)<0)
  539.         {
  540.              Py_XDECREF(t1);
  541.              Py_XDECREF(t2);
  542.             return(NULL);
  543.         }
  544.         Py_DECREF(t1);
  545.         Py_DECREF(t2);
  546.          ix=mod;
  547.     }
  548.     return PyInt_FromLong(ix);
  549. #else
  550.     register long iv, iw, ix;
  551.     iv = v->ob_ival;
  552.     iw = w->ob_ival;
  553.     if (iw < 0) {
  554.         PyErr_SetString(PyExc_ValueError,
  555.                 "integer to the negative power");
  556.         return NULL;
  557.     }
  558.     if ((PyObject *)z != Py_None) {
  559.         PyErr_SetString(PyExc_TypeError,
  560.                 "pow(int, int, int) not yet supported");
  561.         return NULL;
  562.     }
  563.     ix = 1;
  564.     while (--iw >= 0) {
  565.         long prev = ix;
  566.         ix = ix * iv;
  567.         if (iv == 0)
  568.             break; /* 0 to some power -- avoid ix / 0 */
  569.         if (ix / iv != prev)
  570.             return err_ovf("integer pow()");
  571.     }
  572.     return PyInt_FromLong(ix);
  573. #endif
  574. }                
  575.  
  576. static PyObject *
  577. int_neg(v)
  578.     PyIntObject *v;
  579. {
  580.     register long a, x;
  581.     a = v->ob_ival;
  582.     x = -a;
  583.     if (a < 0 && x < 0)
  584.         return err_ovf("integer negation");
  585.     return PyInt_FromLong(x);
  586. }
  587.  
  588. static PyObject *
  589. int_pos(v)
  590.     PyIntObject *v;
  591. {
  592.     Py_INCREF(v);
  593.     return (PyObject *)v;
  594. }
  595.  
  596. static PyObject *
  597. int_abs(v)
  598.     PyIntObject *v;
  599. {
  600.     if (v->ob_ival >= 0)
  601.         return int_pos(v);
  602.     else
  603.         return int_neg(v);
  604. }
  605.  
  606. static int
  607. int_nonzero(v)
  608.     PyIntObject *v;
  609. {
  610.     return v->ob_ival != 0;
  611. }
  612.  
  613. static PyObject *
  614. int_invert(v)
  615.     PyIntObject *v;
  616. {
  617.     return PyInt_FromLong(~v->ob_ival);
  618. }
  619.  
  620. static PyObject *
  621. int_lshift(v, w)
  622.     PyIntObject *v;
  623.     PyIntObject *w;
  624. {
  625.     register long a, b;
  626.     a = v->ob_ival;
  627.     b = w->ob_ival;
  628.     if (b < 0) {
  629.         PyErr_SetString(PyExc_ValueError, "negative shift count");
  630.         return NULL;
  631.     }
  632.     if (a == 0 || b == 0) {
  633.         Py_INCREF(v);
  634.         return (PyObject *) v;
  635.     }
  636.     if (b >= LONG_BIT) {
  637.         return PyInt_FromLong(0L);
  638.     }
  639.     a = (unsigned long)a << b;
  640.     return PyInt_FromLong(a);
  641. }
  642.  
  643. static PyObject *
  644. int_rshift(v, w)
  645.     PyIntObject *v;
  646.     PyIntObject *w;
  647. {
  648.     register long a, b;
  649.     a = v->ob_ival;
  650.     b = w->ob_ival;
  651.     if (b < 0) {
  652.         PyErr_SetString(PyExc_ValueError, "negative shift count");
  653.         return NULL;
  654.     }
  655.     if (a == 0 || b == 0) {
  656.         Py_INCREF(v);
  657.         return (PyObject *) v;
  658.     }
  659.     if (b >= LONG_BIT) {
  660.         if (a < 0)
  661.             a = -1;
  662.         else
  663.             a = 0;
  664.     }
  665.     else {
  666.         if (a < 0)
  667.             a = ~( ~(unsigned long)a >> b );
  668.         else
  669.             a = (unsigned long)a >> b;
  670.     }
  671.     return PyInt_FromLong(a);
  672. }
  673.  
  674. static PyObject *
  675. int_and(v, w)
  676.     PyIntObject *v;
  677.     PyIntObject *w;
  678. {
  679.     register long a, b;
  680.     a = v->ob_ival;
  681.     b = w->ob_ival;
  682.     return PyInt_FromLong(a & b);
  683. }
  684.  
  685. static PyObject *
  686. int_xor(v, w)
  687.     PyIntObject *v;
  688.     PyIntObject *w;
  689. {
  690.     register long a, b;
  691.     a = v->ob_ival;
  692.     b = w->ob_ival;
  693.     return PyInt_FromLong(a ^ b);
  694. }
  695.  
  696. static PyObject *
  697. int_or(v, w)
  698.     PyIntObject *v;
  699.     PyIntObject *w;
  700. {
  701.     register long a, b;
  702.     a = v->ob_ival;
  703.     b = w->ob_ival;
  704.     return PyInt_FromLong(a | b);
  705. }
  706.  
  707. static PyObject *
  708. int_int(v)
  709.     PyIntObject *v;
  710. {
  711.     Py_INCREF(v);
  712.     return (PyObject *)v;
  713. }
  714.  
  715. static PyObject *
  716. int_long(v)
  717.     PyIntObject *v;
  718. {
  719.     return PyLong_FromLong((v -> ob_ival));
  720. }
  721.  
  722. static PyObject *
  723. int_float(v)
  724.     PyIntObject *v;
  725. {
  726.     return PyFloat_FromDouble((double)(v -> ob_ival));
  727. }
  728.  
  729. static PyObject *
  730. int_oct(v)
  731.     PyIntObject *v;
  732. {
  733.     char buf[100];
  734.     long x = v -> ob_ival;
  735.     if (x == 0)
  736.         strcpy(buf, "0");
  737.     else
  738.         sprintf(buf, "0%lo", x);
  739.     return PyString_FromString(buf);
  740. }
  741.  
  742. static PyObject *
  743. int_hex(v)
  744.     PyIntObject *v;
  745. {
  746.     char buf[100];
  747.     long x = v -> ob_ival;
  748.     sprintf(buf, "0x%lx", x);
  749.     return PyString_FromString(buf);
  750. }
  751.  
  752. static PyNumberMethods int_as_number = {
  753.     (binaryfunc)int_add, /*nb_add*/
  754.     (binaryfunc)int_sub, /*nb_subtract*/
  755.     (binaryfunc)int_mul, /*nb_multiply*/
  756.     (binaryfunc)int_div, /*nb_divide*/
  757.     (binaryfunc)int_mod, /*nb_remainder*/
  758.     (binaryfunc)int_divmod, /*nb_divmod*/
  759.     (ternaryfunc)int_pow, /*nb_power*/
  760.     (unaryfunc)int_neg, /*nb_negative*/
  761.     (unaryfunc)int_pos, /*nb_positive*/
  762.     (unaryfunc)int_abs, /*nb_absolute*/
  763.     (inquiry)int_nonzero, /*nb_nonzero*/
  764.     (unaryfunc)int_invert, /*nb_invert*/
  765.     (binaryfunc)int_lshift, /*nb_lshift*/
  766.     (binaryfunc)int_rshift, /*nb_rshift*/
  767.     (binaryfunc)int_and, /*nb_and*/
  768.     (binaryfunc)int_xor, /*nb_xor*/
  769.     (binaryfunc)int_or, /*nb_or*/
  770.     0,        /*nb_coerce*/
  771.     (unaryfunc)int_int, /*nb_int*/
  772.     (unaryfunc)int_long, /*nb_long*/
  773.     (unaryfunc)int_float, /*nb_float*/
  774.     (unaryfunc)int_oct, /*nb_oct*/
  775.     (unaryfunc)int_hex, /*nb_hex*/
  776. };
  777.  
  778. PyTypeObject PyInt_Type = {
  779.     PyObject_HEAD_INIT(&PyType_Type)
  780.     0,
  781.     "int",
  782.     sizeof(PyIntObject),
  783.     0,
  784.     (destructor)int_dealloc, /*tp_dealloc*/
  785.     (printfunc)int_print, /*tp_print*/
  786.     0,        /*tp_getattr*/
  787.     0,        /*tp_setattr*/
  788.     (cmpfunc)int_compare, /*tp_compare*/
  789.     (reprfunc)int_repr, /*tp_repr*/
  790.     &int_as_number,    /*tp_as_number*/
  791.     0,        /*tp_as_sequence*/
  792.     0,        /*tp_as_mapping*/
  793.     (hashfunc)int_hash, /*tp_hash*/
  794. };
  795.  
  796. void
  797. PyInt_Fini()
  798. {
  799. #if NSMALLNEGINTS + NSMALLPOSINTS > 0
  800.     int i;
  801.     PyIntObject **p;
  802.  
  803.     i = NSMALLNEGINTS + NSMALLPOSINTS;
  804.     p = small_ints;
  805.     while (--i >= 0) {
  806.         Py_XDECREF(*p);
  807.         *p++ = NULL;
  808.     }
  809. #endif
  810.     /* XXX Alas, the free list is not easily and safely freeable */
  811. }
  812.